package homework0712;

import java.util.*;

public class HomeWork_2 {
    /**
     * 2. 给定["a", "b", "a", "b", "c", "a", "b", "c", "b"]字符串数组，
     * 然后使用Map的key来保存数组中字符串元素，value保存该字符串元素的出现次数
     * 最后统计出各字符串元素的出现次数。
     */
    public static void main(String[] args) {
      /*  //1.
        String[] str = {"a", "b", "a", "b", "c", "a", "b", "c", "b"};
        Map<String, Integer> map = new HashMap();
        List list = Arrays.asList(str);
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            int count = 0;
            for (int j = 0; j < str.length; j++) {
                if (list.get(i) == str[j]) {
                    count++;
                    map.put(str[j], count);
                }
            }
        }
        System.out.println(map);*/
        //2.
        String[] str = {"a", "b", "a", "b", "c", "a", "b", "c", "b"};
        Map<Object, Integer> map = new HashMap();
        List list = Arrays.asList(str);
        Set set = new HashSet(list);
        for (Object item : set) {
            int count = 0;
            for (int i = 0; i < list.size(); i++) {
                if (item.equals(list.get(i))) {
                    count++;
                }
            }
            map.put(item, count);
        }
        System.out.println(map);
    }
}
